package org.example.repeater.web.controller;

import com.github.benmanes.caffeine.cache.Cache;
import io.netty.channel.ChannelHandlerContext;
import jakarta.annotation.Resource;
import org.example.repeater.RepeaterApplication;
import org.example.repeater.handler.deviceHandler;
import org.example.repeater.server.clientServer;
import org.example.repeater.web.model.Response;
import org.example.repeater.web.model.addConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;

import static org.example.repeater.message.Constant.*;


@RestController
@RequestMapping
public class add {
    private static final Logger log = LoggerFactory.getLogger(RepeaterApplication.class);
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private Cache<String, Object> caffeineCache;

    @PostMapping("addIpAndPort")
    public Mono<Response> T8103(@RequestBody addConfig add) {
        long start = System.currentTimeMillis();
        String key=add.getIp()+":"+add.getPort();
        List<String> value = add.getSim();
        caffeineCache.put(dataPref+key, value);
        redisTemplate.opsForList().leftPushAll(key, value);
        restStart(key,value);
        long end = System.currentTimeMillis();
        long l = (end - start);
        log.info("代码执行耗时:{}毫秒",l);
        return Mono.just(Response.ok("添加成功"));
    }
    public void restStart(String key, List<String> value){
        new Thread(()->{
            try {
                clientServer.close(key,redisTemplate,caffeineCache);
                clientServer.start(key,caffeineCache);
            } catch (InterruptedException e) {
                log.error("808网关连接失败{},原因如下:{}",key,e.getMessage());
            }
        }).start();
        value.forEach(sim->{
            deviceHandler handler = (deviceHandler) caffeineCache.getIfPresent(deivceHandlerPref+sim);
            ChannelHandlerContext ctx = (ChannelHandlerContext) caffeineCache.getIfPresent(clientPref+key);
                if (handler!=null) {
                    if (ctx==null){
                        Object o =  caffeineCache.getIfPresent(clientHandlerPref+key);
                        try {
                            if (o==null){
                                o=new Object();
                                caffeineCache.put(clientHandlerPref+key,o);
                            }
                            synchronized(o){
                                o.wait();
                            }
                            ChannelHandlerContext context = (ChannelHandlerContext) caffeineCache.getIfPresent(clientPref+key);
                            handler.addClientContext(context,key);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        handler.addClientContext(ctx,key);
                    }
                }
            });


    }


//    @PostMapping("restIpAndPort")
//    public Mono<Response> rest(@RequestBody addConfig add) {
//        String address=add.getIp()+":"+add.getPort();
//        List<String> sim = add.getSim();
//        new Thread(()->{
//            try {
//                caffeineCache.put(dataPref+address, sim);
//                clientServer.close(address,redisTemplate,caffeineCache);
//                clientServer.start(address,caffeineCache);
//            } catch (InterruptedException e) {
//                log.error("808网关连接失败{},原因如下:{}",address,e.getMessage());
//            }
//        }).start();
//        return Mono.just(Response.ok(address,"重新连接上级808"));
//    }




}
